import React, { Component } from 'react';
import { Link } from 'react-router-dom'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
import {actions} from '../reducers/index'

import utils from '../common/utils'

class Token extends Component {

    componentDidMount() {
        // console.log(this.props.location.search)
        let token = utils.getQueryString('token'), 
            isNewUser = utils.getQueryString('isNewUser'),
            openid = utils.getQueryString('openid'),
            entry = utils.getQueryString('redirect_url'),
            invite_code = utils.getQueryString('invite_code')

        localStorage.setItem('auth_token', token)
        localStorage.setItem('openid', openid)
        console.log(entry)
        openid&&this.props.update_openid(openid)
        this.props.change_entry(entry)
        
        if (token) {
            if (invite_code) {
                // 通过邀请链接进来的用户,如果已经微信已经绑定了账号，邀请流程结束
                
            } 
            this.props.history.push(entry)
            
        } else {
            if (invite_code && invite_code!='null') {
                this.props.history.push(`/register?invite_code=${invite_code}`)
            } else {
                this.props.history.push("/login")
            }
        }   
        console.log(token, isNewUser)    
    }


    render() {
        return(<div></div>)
    }
}

function mapStateToProps(state) {
    return{

    }
}

function mapDispatchToProps(dispatch) {
    return{
        change_entry: bindActionCreators(actions.change_entry, dispatch),
        update_openid: bindActionCreators(actions.update_openid, dispatch)
    }
}

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Token);